﻿using ServiceMonitor.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ServiceMonitor.Services
{
    /// <summary>
    /// 启动监听的服务抽象服务
    /// </summary>
    public abstract class AbstractStartMonitoringService
    {
        public event EventHandler<ServiceStatusChangedEventArgs> StatusChanged;
        public readonly Dictionary<int, string> _lastStatuses = new Dictionary<int, string>();

        public AbstractStartMonitoringService(EventHandler<ServiceStatusChangedEventArgs> eventHandler)
        {
            StatusChanged = eventHandler;
        }

        public void UpdateStatus(int serviceId, string message)
        {
            // 限制重复消息
            if (_lastStatuses.TryGetValue(serviceId, out string lastStatus) &&
                lastStatus == message)
            {
                return;
            }

            _lastStatuses[serviceId] = message;

            // 触发事件
            var handler = StatusChanged;
            if (handler != null)
            {
                handler(this, new ServiceStatusChangedEventArgs(serviceId, message));
            }
        }
        /// <summary>
        /// 启动监听服务
        /// </summary>
        public abstract void Start(Object obj);
        /// <summary>
        /// 
        /// </summary>
        public virtual void Stop()
        {
            // 默认什么都不做
        }
        /// <summary>
        /// 检查服务状态
        /// </summary>
        /// <param name="state"></param>
        public abstract void CheckServiceStatus(object state);
    }
}
